<extend name="public/base"/>

<block name="body">
    <div class="main-title">
        <h2>新增用户</h2>
    </div>
<div id="page-content" style="min-height: 395px;">
    <div class="row">
        <div class="col-md-6">
           <div class="block full">
                <div class="block-title"><h2>数据库命令</h2></div>
                    <form id="form-sql" onsubmit="return!1" class="form-horizontal form-bordered" method="post" action="{:url('tools/sql?type=save')}">
                        <div class="form-group">
                            <div class="col-md-9 col-md-offset-3">
                                
                                <div class="col-xs-6">
                                    <select size="1" class="form-control" id="tables">
                                        <volist name="tables" id="vo">
                                            <option value="{$vo[$tkey]}">{$vo[$tkey]}</option>
                                        </volist>
                                    </select>
                                </div>
                                <div class="btn-group">                                     
                                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)" aria-expanded="false">快捷SQL语句<span class="caret"></span></a>
                                    <ul class="dropdown-menu text-left">
                                        <li class="dropdown-header"><i class="fa fa-user pull-right"></i><strong>select语句</strong></li>
                                        <li><a href="javascript:insert('SELECT * FROM `[table]` WHERE 1')">select</a></li>
                                        <li><a href="javascript:insert('INSERT INTO `[table]` VALUES ([value-1],[value-2],[value-3],[value-4],[value-5])')">insert</a></li>
                                        <li><a href="javascript:insert('UPDATE `[table]` SET `[]`=[value-2] WHERE 1')">update</a></li>
                                        <li><a href="javascript:insert('DELETE FROM `[table]` WHERE 1')">delete</a></li>                                        
                                        <li class="divider"></li>                                        
                                    </ul>
                                </div>
                                
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="example-textarea-input" class="col-md-3 control-label">SQL命令语句</label>
                            <div class="col-md-9">
                                <textarea placeholder="SQL命令语句" class="form-control" rows="7" name="sql" id="sql"></textarea>
                            </div>
                        </div>
                        <div class="form-group form-actions">
                            <div class="col-md-9 col-md-offset-3">
                                <button class="btn btn-effect-ripple btn-primary" id="form" type="button" style="overflow:hidden;position:relative">提交</button>
                                <button class="btn btn-effect-ripple btn-danger" type="reset" style="overflow:hidden;position:relative">重置</button>
                            </div>
                        </div>
                    </form>
           </div> 
        </div>
        <div class="col-md-6">
            <div class="block full">
                <div class="block-title"><h2>输出结果</h2></div>
                <p id="result" style="background-color: #cccccc;"></p>
           </div>
        </div>
    </div>
    
</div>
</block>

<block name="script">
    <script type="text/javascript">
        //导航高亮
        highlight_subnav('{:U('tools/sql')}');
    </script>
<script src="__JS__/base64.js"></script>
<script>
function insert($sql){
    if ($sql){
        var table=$("#tables").val();
        $("#sql").text($sql.replace("[table]", table));
    }
}
$("#form").click(function(){
    var sql=$("#sql").val();
    var url =$("#form-sql").attr("action");
    if (!sql){jError("SQL不能为空!!", {VerticalPosition: 'center',HorizontalPosition: 'center'});return;}
    sql=base64_encode(sql);
    $.ajax({  
         url:url,
         data:{sql:sql},  
         type:'post',  
         cache:true,  
         dataType:'html',  
         success:function(data) {  
            $("#result").html(data); 
          },  
          error : function() {  
              $("#result").html('错误的SQL语句！<br/>'+$("#sql").val());
              jError("错误的SQL语句！", {VerticalPosition: 'center',HorizontalPosition: 'center'});
          }  
     }); 
      
    
})
</script>
</block>
